package com.xinchao.service.impl;

import com.xinchao.constant.BusinessConstant;
import com.xinchao.constant.DateConstant;
import com.xinchao.constant.RequestParameterConstant;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.dto.*;
import com.xinchao.model.vo.DeviceAvgWatchSumVO;
import com.xinchao.service.AdvertisementMonitorPointService;
import com.xinchao.service.AdvertisementMonitorService;
import com.xinchao.service.DeviceInfoService;
import com.xinchao.util.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 广告监播-点位
 *
 * @author dxy
 * @date 2018/11/12 14:04
 */
@Service(value = "advertisementMonitorPointService")
public class AdvertisementMonitorPointServiceImpl implements AdvertisementMonitorPointService {
	@Autowired
	private AdvertisementMonitorService advertisementMonitorService;
	@Autowired
	private DeviceInfoService deviceInfoService;

	/**
	 * 获取广告的终端曝光次数列表
	 *
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<DeviceExposuresSumDTO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DeviceExposuresSumDTO> listDeviceExposuresSumDTO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = System.currentTimeMillis();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(7, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(30, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//前5条
		Integer size = BusinessConstant.TOP_SIZE;
		return advertisementMonitorService.listDeviceExposuresSumDTO(advertisementName, startTime, endTime, size);
	}

	/**
	 * 获取广告的终端观看人次列表
	 *
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<DeviceWatchSumDTO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DeviceWatchSumDTO> listDeviceWatchSumDTO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = System.currentTimeMillis();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(7, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(30, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//前5条
		Integer size = BusinessConstant.TOP_SIZE;
		return advertisementMonitorService.listDeviceWatchSumDTO(advertisementName, startTime, endTime, size);
	}

	/**
	 * 获取广告的终端平均观看时长列表
	 *
	 * @param advertisementName 广告名称
	 * @param timeType          时间类型
	 * @return List<DeviceAvgWatchSumVO>
	 * @throws ServiceException
	 * @throws ParseException
	 */
	@Override
	public List<DeviceAvgWatchSumVO> listDeviceAvgWatchSumDTO(String advertisementName, Integer timeType) throws ServiceException, ParseException {
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		if (StringUtils.isBlank(advertisementName) || timeType == null) {
			throw new ServiceException("输入参数不能为空");
		}
		//开始时间
		Long startTime = null;
		//结束时间
		Long endTime = System.currentTimeMillis();
		//近7天
		if (RequestParameterConstant.TIME_TYPE_NEAR_SEVEN_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(7, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//近30天
		if (RequestParameterConstant.TIME_TYPE_NEAR_THIRTY_DAY == timeType) {
			String pastDay = DateUtils.getPastDay(30, DateConstant.DATE_SHORT_ISO);
			startTime = DateUtils.stringTimeToLongTimeStamp(pastDay + DateConstant.H_M_S_MIN, DateConstant.LONG_DATE_FORMAT_STR);
		}
		//所有
		List<DevicePlayDurationWatchSumDTO> dtoList = advertisementMonitorService.listDevicePlayDurationWatchSumDTO(advertisementName, startTime, endTime);
		if (CollectionUtils.isEmpty(dtoList)) {
			return null;
		}
		List<DeviceAvgWatchSumVO> voList = new ArrayList<>();
		for (DevicePlayDurationWatchSumDTO dto : dtoList) {
			DeviceAvgWatchSumVO vo = new DeviceAvgWatchSumVO();
			vo.setDetailAddress(dto.getDetailAddress());
			vo.setDeviceNumber(dto.getDeviceNumber());
			Long playDurationSum = dto.getPlayDurationSum();
			Long watchSum = dto.getWatchSum();
			if (watchSum == null || watchSum == 0) {
				vo.setAvgWatchSum(0D);
			} else {
				if (playDurationSum == null || playDurationSum == 0) {
					vo.setAvgWatchSum(0D);
				} else {
					double duration = playDurationSum / 1000 / watchSum;
					String str = String.format("%.2f", duration);
					vo.setAvgWatchSum(Double.valueOf(str));
				}
			}
			voList.add(vo);
		}
		//倒序排序
		Collections.sort(voList);
		//取前5条
		int size = BusinessConstant.TOP_SIZE;
		return voList.subList(0, size);
	}

	/**
	 * 获取广告的城市终端数量列表
	 *
	 * @param advertisementName 广告名称
	 * @return List<CityDeviceSumDTO>
	 * @throws ServiceException
	 */
	@Override
	public List<CityDeviceSumDTO> listCityDeviceSum(String advertisementName) throws ServiceException {
		if (StringUtils.isBlank(advertisementName)) {
			throw new ServiceException("广告名称不能为空");
		}
		//获取广告的终端列表
		List<String> deviceNumberList = advertisementMonitorService.listDeviceSumByAdvertisementName(advertisementName);
		return deviceInfoService.getCityDeviceSumPage(deviceNumberList);
	}

	/**
	 * 获取广告的城市区县终端数量列表
	 *
	 * @param advertisementName 广告名称
	 * @return List<CityRegionDeviceSumDTO>
	 * @throws ServiceException
	 */
	@Override
	public List<CityRegionDeviceSumDTO> listCityRegionDeviceSum(String advertisementName) throws ServiceException {
		if (StringUtils.isBlank(advertisementName)) {
			throw new ServiceException("广告名称不能为空");
		}
		//获取广告的终端列表
		List<String> deviceNumberList = advertisementMonitorService.listDeviceSumByAdvertisementName(advertisementName);
		return deviceInfoService.getCityRegionDeviceSumPage(deviceNumberList);
	}
}
